<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>Tcl_CreateTrace manual page - Library Procedures</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">Tcl C API</a> <small>&gt;</small> CrtTrace</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<DL>
<DD><A HREF="CrtTrace.htm#M2" NAME="L192">NAME</A>
<DL><DD>Tcl_CreateTrace, Tcl_CreateObjTrace, Tcl_DeleteTrace &mdash; arrange for command execution to be traced</DD></DL>
<DD><A HREF="CrtTrace.htm#M3" NAME="L193">SYNOPSIS</A>
<DL>
<DD><B>#include &lt;tcl.h&gt;</B>
<DD>Tcl_Trace
<DD><B>Tcl_CreateTrace</B>(<I>interp, level, proc, clientData</I>)
<DD>Tcl_Trace
<DD><B>Tcl_CreateObjTrace</B>(<I>interp, level, flags, objProc, clientData, deleteProc</I>)
<DD><B>Tcl_DeleteTrace</B>(<I>interp, trace</I>)
</DL>
<DD><A HREF="CrtTrace.htm#M4" NAME="L194">ARGUMENTS</A>
<DL class="arguments">
</DL>
<DD><A HREF="CrtTrace.htm#M5" NAME="L195">DESCRIPTION</A>
<DD><A HREF="CrtTrace.htm#M6" NAME="L196">KEYWORDS</A>
</DL>
<H3><A NAME="M2">NAME</A></H3>
Tcl_CreateTrace, Tcl_CreateObjTrace, Tcl_DeleteTrace &mdash; arrange for command execution to be traced
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>#include &lt;tcl.h&gt;</B><BR>
Tcl_Trace<BR>
<B>Tcl_CreateTrace</B>(<I>interp, level, proc, clientData</I>)<BR>
Tcl_Trace<BR>
<B>Tcl_CreateObjTrace</B>(<I>interp, level, flags, objProc, clientData, deleteProc</I>)<BR>
<B>Tcl_DeleteTrace</B>(<I>interp, trace</I>)<BR>
<H3><A NAME="M4">ARGUMENTS</A></H3>
<DL class="arguments">
<DT><A HREF="../TclLib/Interp.htm">Tcl_Interp</A> <B>*interp</B> (in)<DD>
Interpreter containing command to be traced or untraced.
<P><DT>int <B>level</B> (in)<DD>
Only commands at or below this nesting level will be traced unless
0 is specified.  1 means
top-level commands only, 2 means top-level commands or those that are
invoked as immediate consequences of executing top-level commands
(procedure bodies, bracketed commands, etc.) and so on.
A value of 0 means that commands at any level are traced.
<P><DT>int <B>flags</B> (in)<DD>
Flags governing the trace execution.  See below for details.
<P><DT>Tcl_CmdObjTraceProc <B>*objProc</B> (in)<DD>
Procedure to call for each command that is executed.  See below for
details of the calling sequence.
<P><DT>Tcl_CmdTraceProc <B>*proc</B> (in)<DD>
Procedure to call for each command that is executed.  See below for
details on the calling sequence.
<P><DT>ClientData <B>clientData</B> (in)<DD>
Arbitrary one-word value to pass to <I>objProc</I> or <I>proc</I>.
<P><DT>Tcl_CmdObjTraceDeleteProc <B>*deleteProc</B> (in)<DD>
Procedure to call when the trace is deleted.  See below for details of
the calling sequence.  A NULL pointer is permissible and results in no
callback when the trace is deleted.
<P><DT>Tcl_Trace <B><A HREF="../TclCmd/trace.htm">trace</A></B> (in)<DD>
Token for trace to be removed (return value from previous call
to <B>Tcl_CreateTrace</B>).
<P></DL>
<H3><A NAME="M5">DESCRIPTION</A></H3>
<B>Tcl_CreateObjTrace</B> arranges for command tracing.  After it is
called, <I>objProc</I> will be invoked before the Tcl interpreter calls
any command procedure when evaluating commands in <I>interp</I>.
The return value from <B>Tcl_CreateObjTrace</B> is a token for the trace,
which may be passed to <B>Tcl_DeleteTrace</B> to remove the trace.
There may be many traces in effect simultaneously for the same
interpreter.
<P>
<I>objProc</I> should have arguments and result that match the type,
<B>Tcl_CmdObjTraceProc</B>:
<P>
<PRE>typedef int <B>Tcl_CmdObjTraceProc</B>(
        <B>ClientData</B> <I>clientData</I>,
        <B><A HREF="../TclLib/Interp.htm">Tcl_Interp</A></B>* <I>interp</I>,
        int <I>level</I>,
        const char *<I>command</I>,
        <B><A HREF="../TclLib/CrtObjCmd.htm">Tcl_Command</A></B> <I>commandToken</I>,
        int <I>objc</I>,
        <B><A HREF="../TclLib/Object.htm">Tcl_Obj</A></B> *const <I>objv</I>[]);</PRE>
<P>
The <I>clientData</I> and <I>interp</I> parameters are copies of the
corresponding arguments given to <B>Tcl_CreateTrace</B>.
<I>ClientData</I> typically points to an application-specific data
structure that describes what to do when <I>objProc</I> is invoked.  The
<I>level</I> parameter gives the nesting level of the command (1 for
top-level commands passed to <B><A HREF="../TclLib/Eval.htm">Tcl_Eval</A></B> by the application, 2 for
the next-level commands passed to <B><A HREF="../TclLib/Eval.htm">Tcl_Eval</A></B> as part of parsing or
interpreting level-1 commands, and so on). The <I>command</I> parameter
points to a string containing the text of the command, before any
argument substitution.  The <I>commandToken</I> parameter is a Tcl
command token that identifies the command to be invoked.  The token
may be passed to <B><A HREF="../TclLib/CrtObjCmd.htm">Tcl_GetCommandName</A></B>,
<B><A HREF="../TclLib/CrtObjCmd.htm">Tcl_GetCommandInfoFromToken</A></B>, or <B><A HREF="../TclLib/CrtObjCmd.htm">Tcl_SetCommandInfoFromToken</A></B> to
manipulate the definition of the command. The <I>objc</I> and <I>objv</I>
parameters designate the final parameter count and parameter vector
that will be passed to the command, and have had all substitutions
performed.
<P>
The <I>objProc</I> callback is expected to return a standard Tcl status
return code.  If this code is <B><A HREF="../TclCmd/catch.htm">TCL_OK</A></B> (the normal case), then
the Tcl interpreter will invoke the command.  Any other return code
is treated as if the command returned that status, and the command is
<I>not</I> invoked.
<P>
The <I>objProc</I> callback must not modify <I>objv</I> in any way.  It
is, however, permissible to change the command by calling
<B>Tcl_SetCommandTokenInfo</B> prior to returning.  Any such change
takes effect immediately, and the command is invoked with the new
information.
<P>
Tracing will only occur for commands at nesting level less than
or equal to the <I>level</I> parameter (i.e. the <I>level</I>
parameter to <I>objProc</I> will always be less than or equal to the
<I>level</I> parameter to <B>Tcl_CreateTrace</B>).
<P>
Tracing has a significant effect on runtime performance because it
causes the bytecode compiler to refrain from generating in-line code
for Tcl commands such as <B><A HREF="../TclCmd/if.htm">if</A></B> and <B><A HREF="../TclCmd/while.htm">while</A></B> in order that they
may be traced.  If traces for the built-in commands are not required,
the <I>flags</I> parameter may be set to the constant value
<B>TCL_ALLOW_INLINE_COMPILATION</B>.  In this case, traces on built-in
commands may or may not result in trace callbacks, depending on the
state of the interpreter, but run-time performance will be improved
significantly.  (This functionality is desirable, for example, when
using <B>Tcl_CreateObjTrace</B> to implement an execution time
profiler.)
<P>
Calls to <I>objProc</I> will be made by the Tcl parser immediately before
it calls the command procedure for the command (<I>cmdProc</I>).  This
occurs after argument parsing and substitution, so tracing for
substituted commands occurs before tracing of the commands
containing the substitutions.  If there is a syntax error in a
command, or if there is no command procedure associated with a
command name, then no tracing will occur for that command.  If a
string passed to <A HREF="../TclLib/Eval.htm">Tcl_Eval</A> contains multiple commands (bracketed, or
on different lines) then multiple calls to <I>objProc</I> will occur,
one for each command.
<P>
<B>Tcl_DeleteTrace</B> removes a trace, so that no future calls will be
made to the procedure associated with the trace.  After <B>Tcl_DeleteTrace</B>
returns, the caller should never again use the <I>trace</I> token.
<P>
When <B>Tcl_DeleteTrace</B> is called, the interpreter invokes the
<I>deleteProc</I> that was passed as a parameter to
<B>Tcl_CreateObjTrace</B>.  The <I>deleteProc</I> must match the type,
<B>Tcl_CmdObjTraceDeleteProc</B>:
<P>
<PRE>typedef void <B>Tcl_CmdObjTraceDeleteProc</B>(
        <B>ClientData</B> <I>clientData</I>);</PRE>
<P>
The <I>clientData</I> parameter will be the same as the
<I>clientData</I> parameter that was originally passed to
<B>Tcl_CreateObjTrace</B>.
<P>
<B>Tcl_CreateTrace</B> is an alternative interface for command tracing,
<I>not recommended for new applications</I>.  It is provided for backward
compatibility with code that was developed for older versions of the
Tcl interpreter.  It is similar to <B>Tcl_CreateObjTrace</B>, except
that its <I>proc</I> parameter should have arguments and result that
match the type <B>Tcl_CmdTraceProc</B>:
<P>
<PRE>typedef void <B>Tcl_CmdTraceProc</B>(
        ClientData <I>clientData</I>,
        <A HREF="../TclLib/Interp.htm">Tcl_Interp</A> *<I>interp</I>,
        int <I>level</I>,
        char *<I>command</I>,
        <A HREF="../TclLib/CrtObjCmd.htm">Tcl_CmdProc</A> *<I>cmdProc</I>,
        ClientData <I>cmdClientData</I>,
        int <I>argc</I>,
        const char *<I>argv</I>[]);</PRE>
<P>
The parameters to the <I>proc</I> callback are similar to those of the
<I>objProc</I> callback above. The <I>commandToken</I> is
replaced with <I>cmdProc</I>, a pointer to the (string-based) command
procedure that will be invoked; and <I>cmdClientData</I>, the client
data that will be passed to the procedure.  The <I>objc</I> parameter
is replaced with an <I>argv</I> parameter, that gives the arguments to
the command as character strings.
<I>Proc</I> must not modify the <I>command</I> or <I>argv</I> strings.
<P>
If a trace created with <B>Tcl_CreateTrace</B> is in effect, inline
compilation of Tcl commands such as <B><A HREF="../TclCmd/if.htm">if</A></B> and <B><A HREF="../TclCmd/while.htm">while</A></B> is always
disabled.  There is no notification when a trace created with
<B>Tcl_CreateTrace</B> is deleted.
There is no way to be notified when the trace created by
<B>Tcl_CreateTrace</B> is deleted.  There is no way for the <I>proc</I>
associated with a call to <B>Tcl_CreateTrace</B> to abort execution of
<I>command</I>.
<H3><A NAME="M6">KEYWORDS</A></H3>
<A href="../Keywords/C.htm#command">command</A>, <A href="../Keywords/C.htm#create">create</A>, <A href="../Keywords/D.htm#delete">delete</A>, <A href="../Keywords/I.htm#interpreter">interpreter</A>, <A href="../Keywords/T.htm#trace">trace</A>
<div class="copy">Copyright &copy; 1989-1993 The Regents of the University of California.
<BR>Copyright &copy; 1994-1996 Sun Microsystems, Inc.
<BR>Copyright &copy; 2002 Kevin B. Kenny &lt;kennykb(at)acm.org&gt;. All rights reserved.
</div>
</BODY></HTML>
